import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ml
 * @create 2017-07-29--12:39
 */
public class PFFrame_web {

    private String basePath;
    private String serverIp;

    public PFFrame_web(String bathpath, String serverIp) {
        this.basePath = bathpath;
        this.serverIp = serverIp;

    }

    public void changexml() throws Exception {
        Document doc = XmlUtil.parse(basePath + FilePathEnum.PFFRAME_WEB.getValue());
        updateDocByXPATH(doc);

    }

    public void updateDocByXPATH(Document doc) throws DocumentException, IOException {
        Element root = doc.getRootElement();
        System.out.println(root.getName());

        Map map = new HashMap();
        map.put("title", root.getNamespaceURI());


        XPath xPath = doc.createXPath("//title:filter/title:init-param/title:param-value");

        xPath.setNamespaceURIs(map);

        List<Element> nodes = xPath.selectNodes(doc);

        for (Element element : nodes) {
            Pattern pattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");

            Matcher matcher = pattern.matcher(element.getText());
            if (matcher.find()) {
                String text=matcher.replaceAll(serverIp);
                element.setText(text);
                break;
            }
        }
        XmlUtil.saveDocument(doc,basePath+FilePathEnum.PFFRAME_WEB.getValue());
    }




}
